<template>
  <div style="padding: 20px">
    <el-card>
      <div slot="header" style="position: relative">
        <span>管理员日志</span>
        <div class="topRight" @click="clearLogClick">
          <el-button>清理日志</el-button>
        </div>
      </div>
      <el-row :gutter="10" type="flex" style="margin-top: 20px">
        <el-form ref="form" :model="form" label-width="80px" class="el-form">
          <el-form-item label="管理员:">
            <el-select
              clearable
              v-model="adminLogQuery.adminName"
              placeholder="全部"
              @change="handleSelect"
            >
              <el-option
                v-for="item in manage"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="操作类型:">
            <el-select
              clearable
              v-model="adminLogQuery.type"
              placeholder="全部"
              @change="handleSelectTypeChange"
            >
              <el-option
                v-for="item in operateType"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
             <el-form-item label="关键词:"
            ><el-input clearable v-model="adminLogQuery.keyword" @keyup.enter.native="searchClick" placeholder="请输入关键词"></el-input
          ></el-form-item>
          <el-form-item label="">
            <el-button @click="searchClick">查询</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-table :data="adminLogList" style="width: 100%" max-height="600px">
        <el-table-column prop="addtime" label="时间" width="width">
          <template slot-scope="scope">
            {{ scope.row.addtime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="content" label="操作" width="500">
        </el-table-column>
        <el-table-column prop="type_name" label="类型" width="width">
          <template slot-scope="scope">
            {{ typeDescription[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="adminName" label="管理员" width="width">
        </el-table-column>
        <el-table-column prop="ip" label="IP" width="width"> </el-table-column>
        <el-table-column prop="ipAddr" label="IP归属地" width="width">
        </el-table-column>
      </el-table>
      <el-row :gutter="10" style="margin-top: 20px" type="flex" justify="end">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="adminLogQuery.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="adminLogQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </el-card>

    <!-- 清空日志 -->
    <el-dialog title="清理日志" :visible.sync="clearDialog" width="35%">
      <div>
        <el-form ref="form" label-width="80px">
          <el-form-item label="时间段">
            <el-radio-group v-model="time">
              <el-radio :label="1">一个月前</el-radio>
              <el-radio :label="3">三个月前</el-radio>
              <el-radio :label="6">半年前</el-radio>
              <el-radio :label="12">一年前</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="clearDialog = false">取 消</el-button>
        <el-button type="primary" @click="saveClick">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selAdminLogApi, delAdminLogApi } from "@/api/system";
export default {
  data() {
    return {
      list: [
        {
          id: 2035,
          admin_id: 5,
          admin_name: "admin",
          content: "进入{宁波北仑博视眼科医院有限公司}(企业ID:475)会员中心",
          is_login: 0,
          addtime: 1730269021,
          ip: "221.12.43.38:48072",
          ip_addr: "浙江省宁波市",
          type: 1,
          type_name: "常规",
        },
        {
          id: 2034,
          admin_id: 5,
          admin_name: "admin",
          content: "进入{北仑人才招聘}(企业ID:467)会员中心",
          is_login: 0,
          addtime: 1730267746,
          ip: "221.12.43.38:65376",
          ip_addr: "浙江省宁波市",
          type: 1,
          type_name: "常规",
        },
        {
          id: 2033,
          admin_id: 5,
          admin_name: "admin",
          content:
            "修改{北仑人才招聘}(企业ID:467)的显示状态，显示->不显示，企业及其发布的职位将不对外",
          is_login: 0,
          addtime: 1730267706,
          ip: "221.12.43.38:50394",
          ip_addr: "浙江省宁波市",
          type: 3,
          type_name: "修改",
        },
        {
          id: 2032,
          admin_id: 5,
          admin_name: "admin",
          content: "修改{北仑人才招聘}(企业ID:467)的显示状态，不显示->显示",
          is_login: 0,
          addtime: 1730267704,
          ip: "221.12.43.38:47190",
          ip_addr: "浙江省宁波市",
          type: 3,
          type_name: "修改",
        },
        {
          id: 2031,
          admin_id: 5,
          admin_name: "admin",
          content:
            "查看并审核{北仑人才招聘}(企业ID:467)营业执照，待审核->已认证",
          is_login: 0,
          addtime: 1730267674,
          ip: "221.12.43.38:35014",
          ip_addr: "浙江省宁波市",
          type: 6,
          type_name: "审核",
        },
        {
          id: 2030,
          admin_id: 5,
          admin_name: "admin",
          content:
            "系统-系统管理员-待办事项通知，提醒服务:视频招聘审核通知；启用状态:开启->关闭",
          is_login: 0,
          addtime: 1730267627,
          ip: "60.190.15.252:41192",
          ip_addr: "浙江省宁波市",
          type: 1,
          type_name: "常规",
        },
        {
          id: 2029,
          admin_id: 5,
          admin_name: "admin",
          content:
            "系统-系统管理员-待办事项通知，提醒服务:视频招聘审核通知；启用状态:关闭->开启",
          is_login: 0,
          addtime: 1730267626,
          ip: "60.190.15.252:37684",
          ip_addr: "浙江省宁波市",
          type: 1,
          type_name: "常规",
        },
        {
          id: 2028,
          admin_id: 5,
          admin_name: "admin",
          content: "进入{宁波北仑博视眼科医院有限公司}(企业ID:475)会员中心",
          is_login: 0,
          addtime: 1730266629,
          ip: "221.12.43.38:24790",
          ip_addr: "浙江省宁波市",
          type: 1,
          type_name: "常规",
        },
        {
          id: 2027,
          admin_id: 5,
          admin_name: "admin",
          content: "进入{宁波北仑博视眼科医院有限公司}(企业ID:475)会员中心",
          is_login: 0,
          addtime: 1730255005,
          ip: "221.12.43.38:22204",
          ip_addr: "浙江省宁波市",
          type: 1,
          type_name: "常规",
        },
        {
          id: 2026,
          admin_id: 5,
          admin_name: "admin",
          content: "后台更新缓存",
          is_login: 0,
          addtime: 1730249150,
          ip: "221.12.43.38:57130",
          ip_addr: "浙江省宁波市",
          type: 1,
          type_name: "常规",
        },
      ],
      manage: [
        { value: "", name: "全部" },
        { value: 1, name: "sys" },
        { value: 2, name: "blrc" },
        { value: 3, name: "blrcfwzx" },
        { value: 4, name: "lunchuan" },
        { value: 5, name: "admin" },
      ],
      operateType: [
        { value: "", name: "全部" },
        { value: 1, name: "常规" },
        { value: 2, name: "新增" },
        { value: 3, name: "修改" },
        { value: 4, name: "删除" },
        { value: 5, name: "业务" },
        { value: 6, name: "审核" },
        { value: 7, name: "锁定" },
        { value: 8, name: "导出" },
        { value: 9, name: "特殊" },
      ],
      adminLogQuery: {
        page: 1,
        pageSize: 10,
        adminName: "",
        isLogin: "",
        keyword: "",
        type: null,
      },
      total: "",
      adminLogList: [],
      clearDialog: false,
      time: "",
    };
  },
  created() {
    this.selAdminLog();
  },
  computed: {
    typeDescription() {
      return {
        1: "常规",
        2: "新增",
        3: "修改",
        4: "删除",
        5: "业务",
        6: "审核",
        7: "锁定",
        8: "导出",
        9: "特殊",
      };
    },
  },
  methods: {
    // 清空日志
    saveClick() {
      this.$confirm("删除管理员日志后无法找回，是否确定删除", "警告提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            const res = await delAdminLogApi(this.time);
            console.log(res);
            if (res.code === 1) {
              this.$message.success("清空成功!");
              this.time = "";
              this.clearDialog = false;
            } else {
              this.$message.error("清空失败!");
              this.time = "";
            }
          } catch {
            this.$message.error("清空失败!");
            this.time = "";
          }
        })
        .catch(() => {});
    },
    // 清空日志点击
    clearLogClick() {
      this.clearDialog = true;
    },
    // 选择多少页码
    handleSizeChange(pageSize) {
      this.adminLogQuery.pageSize = pageSize;
      this.selAdminLog();
    },
    // 选择页码
    handleCurrentChange(page) {
      this.adminLogQuery.page = page;
      this.selAdminLog();
    },
    // 管理员下拉
    handleSelect(value) {
      console.log(value);
      this.adminLogQuery.page = 1;
      this.selAdminLog();
    },
    // 操作类型下拉
    handleSelectTypeChange(value) {
      console.log(value);
      this.adminLogQuery.page = 1;
      this.selAdminLog();
    },
    // 查询
    searchClick() {
      this.selAdminLog();
    },
    // 列表渲染那
    async selAdminLog() {
      const res = await selAdminLogApi(this.adminLogQuery);
      console.log(res);
      this.adminLogList = res.records;
      this.total = res.total;
    },
  },
};
</script>

<style lang="scss" scoped>
.el-form {
  display: flex;
}
.topRight {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: 20px;
}
</style>
